package com.czd.support.service;

import com.czd.base.model.Paginate;
import com.czd.base.service.BaseService;
import com.czd.support.common.Constants;
import com.czd.support.model.Room;
import com.czd.support.util.CommonUtils;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomService extends BaseService<Room>{

	public List<Room> findAll(Room room){
		return this.selectList(sqlId("findAllRoom"), room);
	}
	
	public Room findOne(Room room){
		return this.selectOne(sqlId("findRoom"), room);
	}
	
	
	public Room findManagerRoomById(Room room){
		return this.selectOne(sqlId("findManagerRoomById"), room);
	}
	
	public int insert(Room room){
		if(this.insertAndReturnId(sqlId("insertRoom"), room) >0){
            return room.getId();
        }else{
            return 0;
        }
	}
	
	public boolean insertReturnBoolean(Room room){
		return this.insertAndReturnId(sqlId("insertRoom"), room) > 0;
	}
	
	public boolean update(Room room){
		return this.update(sqlId("updateRoom"), room);
	}

	public Paginate queryForPage(Room room) {
		return this.queryForPaginate(sqlId("queryForPage"),room);
	}

	public boolean audit(Room room) {
		return this.update(sqlId("audit"),room);
	}
	
	public boolean updateLiveUser (Room room){
		return this.update(sqlId("updateLiveUser"),room);
	}

    /**
     * 根据 房间分类获取房间id集合
     * @param codes
     * @return
     */
    public List<Integer> queryForCodes(String codes) {
        String [] list = CommonUtils.getArray(codes, Constants.BATCH_SPLIT);
        return this.queryDao.getSqlSession().selectList(sqlId("queryForCodes"), list);
    }

    public boolean syncRoomUserCount(JSONArray jsonArray) {
        return this.update(sqlId("syncRoomUserCount"),jsonArray);
    }

    /**
     * 设置房间的开启和关闭状态
     * @param room
     * @return
     */
    public boolean setEnable(Room room){
        return this.update(sqlId("setEnable"),room);
    }

	/**
	 * 设置内部推广房间
	 * @param room
	 * @return
	 */
	public boolean setInternal(Room room) {
		return this.update(sqlId("setInternal"),room);
	}

    public List<Room> autocompleteById(int id) {
        return this.selectList(sqlId("queryForListById"),id);
    }

    public List<Room> autocompleteByName(String name) {
        return this.selectList(sqlId("queryForListByName"), name);
    }

    public Room findById(int id) {
        return this.selectOne(sqlId("findById"),id);
    }

	public boolean isPlaying(int roomId,JSONArray jsonArray){
		if(jsonArray.size() >0){
			Map<String,Object> m = new HashMap<>();
			m.put("roomId",roomId);
			m.put("userList",jsonArray);
			return  this.count(sqlId("isPlaying"),m) > 0;
		}
		return false;
	}

	public boolean updatePlay(int roomId, boolean play) {
		Map<String,Object> m = new HashMap<>();
		m.put("roomId",roomId);
		m.put("play",play);
		return this.update(sqlId("updatePlay"),m);
	}

	/**
	 * 根据名字查询房间信息
	 * @param name
	 * @return
	 */
	public Room findByName(String name) {
		if (StringUtils.isEmpty(name)) {
			return null;
		}

		return super.selectOne(sqlId("findByName"), name);
	}
}
